// 调用 var uploadJs = require('../../../utils/upload');
// uploadJs.uploadPhoto(e);
var commons = require("./common");
var urls = require("../utils/urls")
export function uploadPhoto(e) { // 拍摄或从相册选取上传
  let that = this;
  wx.chooseImage({
    count: 1, // 默认9
    sizeType: ['compressed'], // 可以指定是原图还是压缩图，默认二者都有
    sourceType: ['album', 'camera'], // 可以指定来源是相册还是相机，默认二者都有
    success(res) {
      let tempFilePaths = res.tempFilePaths; // 返回选定照片的本地路径列表 
      upload(that, tempFilePaths);
    }
  })
}

export function upload(page, path) { // 上传图片
  wx.showToast({
    icon: "loading",
    title: "正在上传……"
  });
  console.log(urls.updateFile,'上传地址')
  return new Promise(function (resolve, reject) {
    wx.uploadFile({
      // url: 'https://recon.nxjdwl.com/blade-resource/oss/endpoint/put-file', //后端接口
      // url: 'https://mtl-app.oss-cn-beijing.aliyuncs.com/blade-resource/oss/endpoint/put-file', //后端接口
      url: urls.updateFile, //后端接口
      filePath: path[0],
      name: 'file',
      header: {
        "Content-Type": "multipart/form-data",
        'Blade-Auth': commons.getToken()
      },
      success: (res) => {
        if (res.statusCode != 200) {
          wx.showModal({
            title: '提示',
            content: '上传失败',
            showCancel: false
          });
          console.log(res.data, '上传图片返回的数据')
          reject(false);
        } else {
          console.log(res, "上传成功");
          resolve(JSON.parse(res.data));
          // console.log("上传成功！ 可对返回的值进行操作，比如：存入imgData；");
        }
      },
      fail(e) {
        console.log(e)
        wx.showModal({
          title: '提示',
          content: '上传失败',
          showCancel: false
        });
        reject(false);
      },
      complete() {
        wx.hideToast(); //隐藏Toast
      }
    })
  })
}

